package com.xjprojectc.base.filter;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 1L;

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void init() {
		// TODO Auto-generated method stub

	}

	public String intercept(ActionInvocation invocation) throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String url = request.getRequestURL().toString();
		if (session.getAttribute("sysUser") == null 
				&& url.indexOf("userLogin")<0 
				&& url.indexOf("Agentaccountinfo_toAdd")<0
				&& url.indexOf("Agentaccountinfo_add")<0
				&& url.indexOf("setLanguage")<0) {
			String flag = "";
			if (request.getHeader("X-Requested-With") != null
					&& request.getHeader("X-Requested-With").equalsIgnoreCase(
							"XMLHttpRequest")) {
				PrintWriter pw = response.getWriter();
				response.setCharacterEncoding("text/html;charset=utf-8");
				response.setContentType("text/html;charset=utf-8");
				flag = "9999";
				pw.write(flag);
				return null;
			} else {
				return "login";
			}
		} 
		return invocation.invoke();
	}

}
